package org.znxs.comfriend.fragments;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.znxs.comfriend.R;
import org.znxs.comfriend.adapters.FriendCardAdapter;
import org.znxs.comfriend.models.User;
import org.znxs.comfriend.utils.AllUserList;
import org.znxs.comfriend.utils.FollowManager;

import java.util.ArrayList;
import java.util.List;

public class FriendsFragment extends Fragment {

    private RecyclerView rvFriends;
    private FriendCardAdapter adapter;
    private List<User> allUsers; // 存储所有用户数据
    private List<User> followingUsers; // 存储关注中的用户数据

    private FollowManager followManager = FollowManager.getInstance(getContext());


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_friends, container, false);

        // 初始化推荐列表视图
        rvFriends = view.findViewById(R.id.rv_friends);
        setupFriendList();

        return view;
    }

    /**
     * 初始化好友列表
     */
    private void setupFriendList() {
        rvFriends.setLayoutManager(new LinearLayoutManager(getContext()));

        // 获取所有用户数据
        allUsers = AllUserList.getInstance().getSharedList();

        // 过滤出正在关注的用户
        followingUsers = filterFollowingUsers(allUsers);

        // 设置适配器
        adapter = new FriendCardAdapter(getContext(), followingUsers);
        rvFriends.setAdapter(adapter);
    }


    /**
     * 过滤出正在关注的用户
     */
    private List<User> filterFollowingUsers(List<User> users) {
        List<User> following = new ArrayList<>();
        List<String> followedUserIds = followManager.getList();

        for (User user : users) {
            if (followedUserIds.contains(user.getId())) {
                following.add(user);
            }
        }

        return following;
    }
}



